/*
 * Copyright (c) 2011-2012, Two Trolls Entertainment
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   * Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   * Neither the name of the Rhenium nor the
 *     names of its contributors may be used to endorse or promote products
 *     derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Two Trolls Entertainment BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#include <SDL/SDL.h>
#include <SDL/SDL_image.h>
#include <SDL/SDL_opengl.h>
#include <iostream>
#include <cmath>
#include "mathhelper.h"
#include "utils.h"
#include "entity.h"

Entity::Entity(){ alive = true;}
Entity::Entity(Vec2F XY) {alive = true; this->XY = XY;}
Entity::Entity(Vec2F nXY, const char *filePath)
{
    XY = nXY;
    Utils::FileManager::loadTextureFromPNG(&texture, filePath);
    alive = true;
}

void Entity::render(){}

void EntityPlayer::render(int delta)
{
    glLoadIdentity();
    glTranslatef(XY.getX(), XY.getY(), 0);
    glEnable(GL_TEXTURE_2D);
    glBlendFunc (GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glEnable (GL_BLEND);

    if (delta - lastFlash > 250)
    {
        firstFrame = !firstFrame;
        lastFlash = delta;
    }

    glBindTexture(GL_TEXTURE_2D, firstFrame ? textures[0] : textures[1]);

    glBegin(GL_QUADS);
        glTexCoord2f(0, 0);
        glVertex2f(0, 0);

        glTexCoord2f(1, 0);
        glVertex2f(59, 0);

        glTexCoord2f(1, 1);
        glVertex2f(59, 55);

        glTexCoord2f(0, 1);
        glVertex2f(0, 55);
    glEnd();
    glDisable(GL_TEXTURE_2D);

}

bool Entity::isAlive()
{
    return alive;
}

void Entity::kill()
{
    alive = false;
}

float Entity::getX()
{
    return XY.getX();
}

float Entity::getY()
{
    return XY.getY();
}

EntityPlayer::~EntityPlayer()
{
    delete inputHandler;
}

void EntityPlayer::tick(int nDelta)
{
    if (inputHandler->upHeld())
        XY.moveY(-(nDelta * speed));
    else if (inputHandler->downHeld())
        XY.moveY(nDelta * speed);
    if (inputHandler->leftHeld())
        XY.moveX(-(nDelta * speed));
    else if (inputHandler->rightHeld())
        XY.moveX(nDelta * speed);

    if (XY.getX() < 0)
        XY.setX(0);
    else if (XY.getX() > 976)
        XY.setX(976);
    if (XY.getY() < 0)
        XY.setY(0);
    else if (XY.getY() > 592)
        XY.setY(592);
}

void EntityPlayer::addInputHandler(int inputType, int inputID)
{
    inputHandler = new Utils::InputHandler(inputType, inputID);
}

void EntityPlayer::handleMovement(SDL_Event event)
{
    inputHandler->tick(event);
}

void EntityBullet::tick(int delta)
{
    XY.moveXY(angle, speed, delta);

    if (XY.getX() < 0 || XY.getX() > 1024 || XY.getY() < 0 || XY.getY() > 640)
        kill();
}

void EntityBullet::render()
{
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    glBindTexture(GL_TEXTURE_2D, texture);

    //glPushMatrix();
        glLoadIdentity();
        glTranslatef(XY.getX(), XY.getY(), 0.0f);
        glRotatef(-angle + 90, 0.0f, 0.0f, 1.0f);

        glBegin(GL_QUADS);
            glTexCoord2f(0.0f, 0.0f);
            glVertex2f(0.0f, 0.0f);

            glTexCoord2f(1.0f, 0.0f);
            glVertex2f(10.0f, 0.0f);

            glTexCoord2f(1.0f, 1.0f);
            glVertex2f(10.0f, 15.0f);

            glTexCoord2f(0.0f, 1.0f);
            glVertex2f(0.0f, 15.0f);
        glEnd();
    //glPopMatrix();
    glDisable(GL_TEXTURE_2D);
}

void EntityManager::tick(int delta)
{
    if (bullets.size() > 0)
    {
        //Update all bullets
        for (unsigned int i=0; i < bullets.size(); i++)
        {
            if (bullets[i].isAlive())
                bullets[i].tick(delta);
            else
                bullets.erase(bullets.begin() + i);
        }
    }
}

void EntityManager::addBullet(EntityBullet bullet)
{
    bullets.push_back(bullet);
}

void EntityManager::render()
{
    if (bullets.size() > 0)
    {
        for (unsigned int i=0; i < bullets.size(); i++)
        {
            bullets[i].render();
        }
    }
}
